package com.novellotus.iov.carvice.activity;

import android.os.Bundle;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

import com.novellotus.httploadingview.LoadingView;
import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.adapter.TaskAdapter;
import com.novellotus.iov.carvice.bean.RxTaskListBean;
import com.novellotus.iov.carvice.retrofit.BaseEntity;
import com.novellotus.iov.carvice.retrofit.BaseObserver;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.retrofit.RxSchedulers;
import com.novellotus.iov.carvice.utils.MyViewUtils;
import com.novellotus.iov.carvice.utils.SharedPreferencesUtils;
import com.novellotus.iov.carvice.view.PageView;
import com.orangegangsters.github.swipyrefreshlayout.library.RefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.RefreshLayoutDirection;

/**
 * 任务列表页面
 * */
public class TaskActivity extends BaseActivity implements TaskAdapter.ItemClickInterface, RefreshLayout.OnRefreshListener, LoadingView.OnBtnClickListener, PageView.PageViewInterface {

    @BindView(R.id.pageView)
    PageView pageView;
    TaskAdapter taskAdapter;
    private final int PAGE_SIZE = 10;

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_task);
    }

    @Override
    public void init() {
        pageView.setLoadFinish(false);
        pageView.initLoadingView(this);
        pageView.refreshLayout.setOnRefreshListener(this);
        pageView.setPageViewInterface(this);
        pageView.loadingView.setOnBtnClickListener(this);
        pageView.loadingView.mWarnView.setTextColor(getResources().getColor(R.color.text_black));
        getTaskList(1);
    }

    public void getTaskList(int currentPate) {
        Observable<BaseEntity<RxTaskListBean>> observable = RetrofitFactory.getInstance().getTaskList(SharedPreferencesUtils.getKey(this)[0], "", PAGE_SIZE, currentPate);
        observable.compose(RxSchedulers.<BaseEntity<RxTaskListBean>>compose())
                .subscribe(new BaseObserver<RxTaskListBean>(this) {
                    @Override
                    public void onSuccess(RxTaskListBean response) {

                        if (response.getTotal() == 0) {
                            pageView.loadFail("暂无维修记录", false);
                        } else {
                            pageView.loadSuccess();
                            pageView.setTotalPage(response.getTotal(), PAGE_SIZE);
                            taskAdapter = new TaskAdapter(TaskActivity.this, response.getRecords());
                            pageView.setBottomInterface(taskAdapter);
                            pageView.setRecyclerViewAdapter(MyViewUtils.getLinearLayoutManager(TaskActivity.this), taskAdapter);
                        }
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        pageView.loadFail(msg, true);
                    }

                    @Override
                    public void onError(String msg) {
                        pageView.loadFail("加载失败", true);
                    }

                });
    }

    @OnClick(R.id.img_back)
    void back() {
        finish();
    }

    @OnClick(R.id.img_search)
    void clickSearch() {
        openActivity(SearchTaskActivity.class);
    }

    @Override
    public void clickItem(RxTaskListBean.RecordsBean record) {
        Bundle bundle = new Bundle();
        bundle.putString("fid", record.getId());
        bundle.putString("plate", record.getPn());
        bundle.putString("vin", record.getVin());
        bundle.putString("statue", record.getStatus());
        //  bundle.putString("name", record.getStaff().getName());
        //   bundle.putString("mobile", record.getStaff().getPhone());
        openActivity(TaskDetailActivity.class, bundle);
    }

    @Override
    public void loadData(int dataPage) {
        getTaskList(pageView.currentPage);
    }

    @Override
    public void onReload() {
        pageView.setLoadCurrentPage(1);
        getTaskList(pageView.getLoadCurrentPage());
    }

    @Override
    public void onRefresh(RefreshLayoutDirection direction) {
        pageView.refreshData(direction);
    }
}
